import urllib.request
import urllib.parse
import urllib.error
import json
# 适用的场景：数据采集的时候需要绕过登陆然后进入到某个页面
# 个人信息页面是utf-8 但是还报错了编码错误
# 因为并没有进入到个人信息页面 而是跳转到了登陆页面 那么登陆页面不是utf-8所以报错

url = 'https://cart.taobao.com/cart.htm'  # 淘宝购物车（原定测试的是微博页面）

headers = {
    # ':authority': 'h5api.m.taobao.com',
    # ':method': 'POST',
    # ':path': '/h5/mtop.trade.query.bag/5.0/?jsv=2.7.2&appKey=12574478&t=1728546606601&sign=072fd4a172c6566bb48351fd6e0cb9d4&v=5.0&ecode=1&timeout=8000&dataType=json&valueType=original&ttid=1%40tbwang_windows_1.0.0%23pc&needLogin=true&type=originaljson&isHttps=1&api=mtop.trade.query.bag&preventFallback=true',
    # ':scheme': 'https',
    'accept': 'application/json',
    # 'accept-encoding': 'gzip, deflate, br, zstd',
    'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'content-length': '347',
    'content-type': 'application/x-www-form-urlencoded',
    # cookie中携带着你的登陆信息如果有登陆之后的cookie 那么我们就可以携带着cookie进入到任何页面
    'cookie': 'thw=cn; t=23c0c50b61858303b509f3ae5834bbef; hng=CN%7Czh-CN%7CCNY%7C156; _uetvid=89fd15e0500b11ef8886579c5bff040a; tracknick=%5Cu6DE1%5Cu5FD8%5Cu6D45%5Cu964C%5Cu9621%5Cu964C; mtop_partitioned_detect=1; _m_h5_tk=6ea906232eab83167954604a7c5295ff_1728555505275; _m_h5_tk_enc=22faa7db7d339ab5cec773f6a20073f8; _tb_token_=e1e3e354648fa; xlly_s=1; cookie2=1ae954a4069b3949c2b762034e77d071; _samesite_flag_=true; 3PcFlag=1728546516765; sgcookie=E100GRiV1Yb%2FzZL0U38Kz8MFF%2BRAWaCRcIk1wloU51dtQkya9vM0kSc4YKYzC78mZ5CWqAKdm3otMZBDYm7gn%2BCTHwb1NIfbfZfzYCUmFWFFPbA%3D; wk_cookie2=1a4f567957a5a3a7b32a3a17e752b279; wk_unb=UU8IOfyThKdC4Q%3D%3D; unb=2705499723; uc3=vt3=F8dD37ng4aD5omuyPgo%3D&id2=UU8IOfyThKdC4Q%3D%3D&lg2=U%2BGCWk%2F75gdr5Q%3D%3D&nk2=1uKPeijg0v6iar9v; csg=d28c59f7; lgc=%5Cu6DE1%5Cu5FD8%5Cu6D45%5Cu964C%5Cu9621%5Cu964C; cancelledSubSites=empty; cookie17=UU8IOfyThKdC4Q%3D%3D; dnk=%5Cu66FC%5Cu8BFA%5Cu5C14%5Cu96F7%5Cu8FEA%5Cu4E9A%5Cu5179; skt=276535b795d894f3; existShop=MTcyODU0NjU0NA%3D%3D; uc4=id4=0%40U22PHHaOiCOYQ9YYLOdviwiItewj&nk4=0%401FOYNoB5cuw3isOPB0C8P7FPi5LfHjU%3D; _cc_=UIHiLt3xSw%3D%3D; _l_g_=Ug%3D%3D; sg=%E9%99%8C39; _nk_=%5Cu6DE1%5Cu5FD8%5Cu6D45%5Cu964C%5Cu9621%5Cu964C; cookie1=BxfQYn0AWRYmPXfqLYdN%2FoKBN1e8yb0oQSCTXv5b13c%3D; isg=BN3d6_oZZnsNJAMgV0Ecnhx57LnX-hFMX3Dc7Z-jWDRjVv-IZ0lRHLnFhErQlikE; uc1=existShop=false&cookie14=UoYcC%2FHczJg1%2FA%3D%3D&pas=0&cookie15=W5iHLLyFOGW7aA%3D%3D&cookie21=U%2BGCWk%2F7p4mBoUyS4E9C&cookie16=W5iHLLyFPlMGbLDwA%2BdvAGZqLg%3D%3D; tfstk=gijtwt_37WViXiDE0jaHom17QgyhXiBNp1WSmIAil6CdGspmmtODDsCAMCYb_hjA9s1vnmXfmKsvbMbimCVwDt1kD7VuquXNQE-bZ7D03jPXcKZDGubgpZ0Xc7VuqRUEw3-fs1Idhk3BUB96hCObpH9DHmtX5COQpK9m1qi1G9wpTLgsfdO6RH92enOfGnwddjkSBID6MmQ2CwVmG7rEDmIpBFdxmBnANMmkWQ1H9mN5vdhXNOOKcmOMKyuhpOVt49xNc_plskiOwt6PleIxO7dVv9s1l9lTd3Bd-aYOJ5ivtGvvPnTKhm6pcNTcf3ZS9p7CIZsN6xnXQGjkkQ8Lhm8DAgY5P1H4U9t6hs8Pm7ow1t6P4ax8XbJORTKd4ijl2o9mZQpm5JeKuq86LOB6PVQ8asN9pQ2n-qu2-pJpZJeKuq86LpduKk0quevF.',
    'origin': 'https://cart.taobao.com',
    'priority': 'u=1, i',
    # referer可以拿来防盗链（判断当前路劲是不是上一个链接进来的）（图片防盗链）
    'referer': 'https//cart.taobao.com/',
    'sec-ch-ua': '"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
}

request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(request)
data = response.read().decode('utf-8')
with open("cookie.html", "w", encoding="utf-8") as f:
    f.write(data)
